/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.monitor.profile.command.base;

import com.hyts.monitor.profile.command.bean.AccessResult;
import com.hyts.monitor.profile.command.filter.SessionFilter;
import com.hyts.monitor.profile.command.server.SessionManager;
import com.hyts.monitor.profile.config.ProfilerConfig;
import com.hyts.monitor.profile.model.Connector;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.monitor.profile.command
 * @author:LiBo/Alex
 * @create-date:2019-08-09 15:14
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public abstract class SessionCommandLine implements CommandLine {

    /**
     * 连接器操作服务对象
     */
    @Getter
    Connector connector;

    final SessionManager sessionFilter = new SessionManager();

    /**
     * 执行命令操作
     * @param connector
     * @param commandStr
     * @return
     */
    @Override
    public AccessResult execute(Connector connector, ProfilerConfig.CommandConfig commandConfig, String commandStr) {
        //服务设置开关切换
        //匹配不同类型的command
        boolean author = sessionFilter.containSessionId(connector.getSessionId());
        if(commandConfig == ProfilerConfig.CommandConfig.LOGIN || author) {
            switch (commandConfig) {
                case LOGIN:
                    AccessResult accessResult = login(connector);
                    sessionFilter.addSessionId(accessResult.getSessionId());
                    return accessResult;
                case LOGOUT:
                    return logout(connector);
                case STATUS:
                    return status(connector, commandStr);
                case FLUSH:
                    return flush(connector);
                case NOVAL:
                    return nonval(connector);
                default:
                    return nonval(connector);
            }
        }
        return null;
    }

    public abstract AccessResult login(Connector connector);

    public abstract AccessResult logout(Connector connector);

    public abstract AccessResult status(Connector connector,String commandStr);

    public abstract AccessResult flush(Connector connector);

    public abstract AccessResult nonval(Connector connector);

}
